package com.cn.ycy.controller;

import com.cn.ycy.config.interceptor.SecureUtil;
import com.cn.ycy.dto.*;
import com.cn.ycy.entity.result.R;
import com.cn.ycy.service.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 演示PageHelper
 * 测试 valid
 */
@Controller
@RequestMapping("pagehelper/user")
@Slf4j
public class PageHelperController {

    @Autowired
    private UserService userService;

    public static Map<String, User> loginToken = new HashMap<>();

    @PostMapping(value = "/login")
    @ResponseBody
    public R<User> login(HttpServletResponse response, HttpServletRequest request,
                         @RequestBody UserForLogin userForLogin) {
        if (StringUtils.equals(userForLogin.getUserName(), "tom")
                && StringUtils.equals(userForLogin.getPassWord(), "1")) {
            User user = new User();
            user.setUserId(1);
            user.setUsername("Tom");
            user.setSex("F");
            //生成uuid、cookie
            String uuid = UUID.randomUUID().toString().replace("-", "");
            user.setToken(uuid);
            //urls
            user.setMenus(getMenus());
            //tenant
            user.setTenants(getTenant());
            user.setRoles(new String[]{"admin"});
            //cache
            loginToken.put(uuid, user);
            return R.success(user);
        }else if (StringUtils.equals(userForLogin.getUserName(), "james")
                && StringUtils.equals(userForLogin.getPassWord(), "1")){
            User user = new User();
            user.setUserId(1);
            user.setUsername("James");
            user.setSex("F");
            //生成uuid、cookie
            String uuid = UUID.randomUUID().toString().replace("-", "");
            user.setToken(uuid);
            //urls
            user.setMenus(getMenus());
            //tenant
            user.setTenants(getTenant());
            user.setRoles(new String[]{"user"});
            //cache
            loginToken.put(uuid, user);
            return R.success(user);
        }
        return R.failure("username or password error");
    }

    @GetMapping(value = "/loginout")
    @ResponseBody
    public R<String> loginOut(HttpServletResponse response) {
        User user = SecureUtil.getUser();
        if (user != null) {
            String token = user.getToken();
            loginToken.remove(token);
            return R.successWithData("login success");
        }
        return R.successWithData("not find user");
    }

    private List<Menus> getMenus() {
        List<Menus> list = new ArrayList<>();

        Menus layer = new Menus();
        layer.setUrl("/chart");
        layer.setDesc("ChartOverview");
        list.add(layer);

        Menus rootMenus = new Menus();
        rootMenus.setUrl("/sys");
        rootMenus.setDesc("SysManager");
        list.add(rootMenus);

        Menus database = new Menus();
        database.setUrl("/inventoryview");
        database.setDesc("InventoryView");
        list.add(database);

        return list;
    }

    private List<Tenant> getTenant() {
        List<Tenant> list = new ArrayList<>();
        Tenant rootTenants = new Tenant();
        rootTenants.setCode("001");
        rootTenants.setDesc("xinhaowuye");
        list.add(rootTenants);

        Tenant otherTenants = new Tenant();
        otherTenants.setCode("002");
        otherTenants.setDesc("xinhaoguanchang");
        list.add(otherTenants);

        return list;
    }


    @GetMapping(value = "/page")
    @ResponseBody
    public R<PageInfo<User>> selectPage(int pageNum, int pageSize, String username) {
        try {
            //模拟耗时 500ms
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        PageInfo<User> userPageInfo = userService.selectPage(pageNum, pageSize, username);
        return R.success(userPageInfo);
    }

    @PostMapping(value = "/update")
    @ResponseBody
    public R<Boolean> update(@RequestBody User user) {
        try {
            //模拟耗时 500ms
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Boolean flag = userService.update(user);
        return R.success(flag);
    }

    @PostMapping(value = "/insert")
    @ResponseBody
    public R<Boolean> insert(@RequestBody User user) {
        Boolean flag = userService.insert(user);
        return R.success(flag);
    }

    @GetMapping(value = "/delete")
    @ResponseBody
    public R<Boolean> delete(int userId) {

        try {
            //模拟耗时 500ms
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        User user = new User();
        user.setUserId(userId);
        Boolean delete = userService.delete(user);
        return R.success(true);
    }

    @GetMapping(value = "/sexType")
    @ResponseBody
    public R<List<Map<String, String>>> sexType() {
        List<Map<String, String>> res = new ArrayList<>();
        Map<String, String> f = new HashMap<>();
        f.put("label", "M");
        f.put("value", "2");
        res.add(f);

        Map<String, String> m = new HashMap<>();
        m.put("value", "1");
        m.put("label", "F");
        res.add(m);
        return R.success(res);
    }


}
